﻿using UnityEngine;
using UnityEditor;
using System.Collections;
using System.IO;
using System.Text;
using System.Collections.Generic;
using System;
using System.Diagnostics;

public class CYtoolsMiniObject : MonoBehaviour
{
    enum SnapType
    {
        StAll,//所有
        StWidget,//挂件   
        StSprite,//图片
    }
    //&%#s
    [MenuItem(" | CYtools | / *hideobjects  #a")]
    static void HideObj()
    {
        HideYou();
    }

    [MenuItem(" | CYtools | /  Snap all #s")]
    static void SnapObjAll()
    {
        SnapGameObject();
    
    }

    [MenuItem(" | CYtools | /  Snap widget #x")] 
    static void SnapObjWidget()
    {
        //SnapMe(SnapType.StWidget);
    }
	
	[MenuItem(" | CYtools | /GAME截屏")]
    private static void CaptureScreen()
    {
        string tname = Application.dataPath.Replace("Assets", "") + "Screen" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".png";
        UnityEngine.Debug.Log(tname);
        #if (UNITY_5_3 || UNITY_5_4 || UNITY_5_5 || UNITY_5_6 || UNITY_5_7 || UNITY_5_8 || UNITY_5_9)
        Application.CaptureScreenshot(tname, 0);
        #else
        ScreenCapture.CaptureScreenshot(tname, 0);
        #endif
        // Process pp = Process.Start(tname);
    }
	
    static void HideYou()
    {
        GameObject[] objects = Selection.gameObjects;
        foreach (GameObject o in objects)
        {
            if (o.activeSelf == true)
            {
                o.SetActive(false);
            }
            else
            {
                o.SetActive(true);
            }

        }

    }
    /*
    static void SnapMe(SnapType st)
    {

        GameObject[] objects = Selection.gameObjects;

        switch (st)
        {
            case SnapType.StWidget:
                foreach (GameObject go in objects)
                {
                    UIWidget pw = go.GetComponent<UIWidget>();

                    if (pw != null)
                    {
                        NGUIEditorTools.RegisterUndo("Snap Dimensions", pw);
                        NGUIEditorTools.RegisterUndo("Snap Dimensions", pw.transform);
                        pw.MakePixelPerfect();
                    }
                }
                break;
            case SnapType.StAll:
                foreach (GameObject go in objects)
                {

                    Transform[] traf = go.GetComponentsInChildren<Transform>(true);
                    if (traf.Length < 1)
                        return;
                    foreach (Transform tr in traf)
                    {
                        Vector3 pos = tr.localPosition;
                        pos.z = Mathf.Round(pos.z);
                        pos.x = Mathf.Round(pos.x);
                        pos.y = Mathf.Round(pos.y);
                        tr.localPosition = pos;

                        Vector3 ls = tr.localScale;
                        ls.z = Mathf.Round(ls.z);
                        ls.x = Mathf.Round(ls.x);
                        ls.y = Mathf.Round(ls.y);
                        tr.localScale = ls;
                    }
                }
                break;
            case SnapType.StSprite:
                foreach (GameObject go in objects)
                {
                    UISprite ps = go.GetComponent<UISprite>();

                    if (ps != null)
                    {
                        NGUIEditorTools.RegisterUndo("Snap Dimensions", ps);
                        NGUIEditorTools.RegisterUndo("Snap Dimensions", ps.transform);
                        ps.MakePixelPerfect();
                    }
                }
                break;

        }
        //AssetDatabase.SaveAssets();
    }*/
    private static void SnapGameObject()
    {
        GameObject[] objects = Selection.gameObjects;
        foreach (GameObject o in objects)
        {
            Transform[] tran = o.GetComponentsInChildren<Transform>(true);
            UnityEngine.Debug.Log(tran.Length);
            foreach (Transform tr in tran)
            {
                tr.localPosition = SnapVector3(tr.localPosition);
                tr.localScale = SnapVector3(tr.localScale);
                string str = "name:" + tr.name + "P:" + tr.localPosition + "R:" + tr.localRotation + "S:" + tr.localScale;
                UnityEngine.Debug.Log(str);
            }
        }
    }
    private static Vector3 SnapVector3(Vector3 vt3)
    {
        Vector3 v3 = Vector3.zero;
        v3.x = Mathf.RoundToInt(vt3.x);
        v3.y = Mathf.RoundToInt(vt3.y);
        v3.z = Mathf.RoundToInt(vt3.z);
        return v3;
    }

}
